package com.ruoyi.bantong.service.impl;

import java.util.List;

import com.ruoyi.bantong.domain.BtPlacecardBlacklist;
import com.ruoyi.bantong.domain.BtPlacecardConfig;
import com.ruoyi.bantong.domain.BtPlacecardPool;
import com.ruoyi.bantong.mapper.BtPlacecardBlacklistMapper;
import com.ruoyi.bantong.mapper.BtPlacecardConfigMapper;
import com.ruoyi.bantong.mapper.BtPlacecardPoolMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtPlacecardPoolDetailsMapper;
import com.ruoyi.bantong.domain.BtPlacecardPoolDetails;
import com.ruoyi.bantong.service.IBtPlacecardPoolDetailsService;

/**
 * 席位玩法详情Service业务层处理
 * 
 * @author laohei
 * @date 2021-01-24
 */
@Service
public class BtPlacecardPoolDetailsServiceImpl implements IBtPlacecardPoolDetailsService 
{
    @Autowired
    private BtPlacecardPoolDetailsMapper btPlacecardPoolDetailsMapper;

    @Autowired
    private BtPlacecardPoolMapper poolMapper;

    @Autowired
    private BtPlacecardConfigMapper configMapper;

    @Autowired
    private BtPlacecardBlacklistMapper blacklistMapper;

    /**
     * 查询席位玩法详情
     * 
     * @param ppdId 席位玩法详情ID
     * @return 席位玩法详情
     */
    @Override
    public BtPlacecardPoolDetails selectBtPlacecardPoolDetailsById(Integer ppdId)
    {
        return btPlacecardPoolDetailsMapper.selectBtPlacecardPoolDetailsById(ppdId);
    }

    @Override
    public List<BtPlacecardPoolDetails> selectBtPlacecardPoolDetailsListByUserId(BtPlacecardPoolDetails btPlacecardPoolDetails) {
        return btPlacecardPoolDetailsMapper.selectBtPlacecardPoolDetailsListByUserId(btPlacecardPoolDetails);
    }

    /**
     * 查询席位玩法详情列表
     * 
     * @param btPlacecardPoolDetails 席位玩法详情
     * @return 席位玩法详情
     */
    @Override
    public List<BtPlacecardPoolDetails> selectBtPlacecardPoolDetailsList(BtPlacecardPoolDetails btPlacecardPoolDetails)
    {
        return btPlacecardPoolDetailsMapper.selectBtPlacecardPoolDetailsList(btPlacecardPoolDetails);
    }

    /**
     * 新增席位玩法详情
     * 
     * @param btPlacecardPoolDetails 席位玩法详情
     * @return 结果
     */
    @Override
    public int insertBtPlacecardPoolDetails(BtPlacecardPoolDetails btPlacecardPoolDetails)
    {
        btPlacecardPoolDetails.setCreateTime(DateUtils.getNowDate());
        return btPlacecardPoolDetailsMapper.insertBtPlacecardPoolDetails(btPlacecardPoolDetails);
    }

    /**
     * 修改席位玩法详情
     * 
     * @param btPlacecardPoolDetails 席位玩法详情
     * @return 结果
     */
    @Override
    public int updateBtPlacecardPoolDetails(BtPlacecardPoolDetails btPlacecardPoolDetails)
    {
        Integer result = btPlacecardPoolDetails.getResult();
        Integer ppdId = btPlacecardPoolDetails.getPpdId();
        BtPlacecardPoolDetails details = btPlacecardPoolDetailsMapper.selectBtPlacecardPoolDetailsById(ppdId);
        Integer ppId = details.getPpId();
        BtPlacecardPool pool = poolMapper.selectBtPlacecardPoolById(ppId);
        Integer level = pool.getLevel();
        BtPlacecardConfig btPlacecardConfig = configMapper.selectBtPlacecardConfigById(level);
        Integer winConut = btPlacecardPoolDetailsMapper.selectWinCountByPpId(ppId);
        BtPlacecardBlacklist black = new BtPlacecardBlacklist();
        black.setUserId(details.getUserId());
        Integer count = blacklistMapper.selectBlackListByUser(details.getUserId(), level);
        if(count > 0){
            throw new RuntimeException("该用户在黑名单中！");
        }
        Integer winNum = btPlacecardConfig.getWinCount();
        if(result == 1 && (winConut + 1) > winNum){
            throw new RuntimeException("中奖人数超过规定人数");
        }
        btPlacecardPoolDetails.setUpdateTime(DateUtils.getNowDate());
        return btPlacecardPoolDetailsMapper.updateBtPlacecardPoolDetails(btPlacecardPoolDetails);
    }

    /**
     * 批量删除席位玩法详情
     * 
     * @param ppdIds 需要删除的席位玩法详情ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardPoolDetailsByIds(Integer[] ppdIds)
    {
        return btPlacecardPoolDetailsMapper.deleteBtPlacecardPoolDetailsByIds(ppdIds);
    }

    /**
     * 删除席位玩法详情信息
     * 
     * @param ppdId 席位玩法详情ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardPoolDetailsById(Integer ppdId)
    {
        return btPlacecardPoolDetailsMapper.deleteBtPlacecardPoolDetailsById(ppdId);
    }
}
